package com.ruoyi.business.controller;

import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.BussEnvIntro;
import com.ruoyi.business.service.IBussEnvIntroService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 区域营商环境介绍Controller
 *
 * @author MagicCube
 * @date 2023-04-28
 */
@RestController
@RequestMapping("/businessIntro")
public class BussEnvIntroController extends BaseController
{
    @Autowired
    private IBussEnvIntroService bussEnvIntroService;

    @Autowired
    private RedisService redisService;
    /**
     * 查询区域营商环境介绍列表
     */
    @RequiresPermissions(value = {"business:businessIntro:list","business:busfrontIntro:list"},logical = Logical.OR)
    @GetMapping("/list")
    public TableDataInfo list(BussEnvIntro bussEnvIntro)
    {
        startPage();
        bussEnvIntro.setGovernId(SecurityUtils.getUserId());
        List<BussEnvIntro> list = bussEnvIntroService.selectBussEnvIntroList(bussEnvIntro);
        return getDataTable(list);
    }

    /**
     * 查询区域营商环境介绍列表
     */
    @RequiresPermissions(value = {"business:businessIntro:list","business:busfrontIntro:list"},logical = Logical.OR)
    @GetMapping("/allList")
    public TableDataInfo allList(BussEnvIntro bussEnvIntro)
    {
        startPage();
        List<BussEnvIntro> list = bussEnvIntroService.selectBussEnvIntroList(bussEnvIntro);
        return getDataTable(list);
    }

    /**
     * 导出区域营商环境介绍列表
     */
    @RequiresPermissions(value = {"business:businessIntro:export","business:busfrontIntro:export"},logical = Logical.OR)
    @Log(title = "区域营商环境介绍", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BussEnvIntro bussEnvIntro)
    {
        List<BussEnvIntro> list = bussEnvIntroService.selectBussEnvIntroList(bussEnvIntro);
        ExcelUtil<BussEnvIntro> util = new ExcelUtil<BussEnvIntro>(BussEnvIntro.class);
        util.exportExcel(response, list, "区域营商环境管理数据");
    }
    @RequiresPermissions(value = {"business:businessIntro:export","business:busfrontIntro:export"},logical = Logical.OR)
    @Log(title = "区域营商环境介绍", businessType = BusinessType.EXPORT)
    @PostMapping("/userExport")
    public void userExport(HttpServletResponse response, BussEnvIntro bussEnvIntro)
    {
        bussEnvIntro.setGovernId(SecurityUtils.getUserId());
        List<BussEnvIntro> list = bussEnvIntroService.selectBussEnvIntroList(bussEnvIntro);
        ExcelUtil<BussEnvIntro> util = new ExcelUtil<BussEnvIntro>(BussEnvIntro.class);
        util.exportExcel(response, list, "区域营商环境管理数据");
    }

    /**
     * 下载模板
     * @param response response
     * @throws IOException
     */
//    @RequiresPermissions(value = {"business:businessIntro:export","business:busfrontIntro:export"},logical = Logical.OR)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        List<BussEnvIntro> list = new ArrayList<>();
        BussEnvIntro bussEnvIntro = new BussEnvIntro();
//        bussEnvIntro.setProvinceId(34L);
        bussEnvIntro.setProvince("北京");
//        bussEnvIntro.setCityId(428L);
        bussEnvIntro.setCity("北京市");
//        bussEnvIntro.setAreaId(3711L);
        bussEnvIntro.setArea("朝阳区");
        bussEnvIntro.setGovernId(1L);
        bussEnvIntro.setBussEconomic("长江三角洲区域一体化,一带一路");
        bussEnvIntro.setDevelopType("高新技术开发区");
        bussEnvIntro.setParkType("高新区");
        bussEnvIntro.setGovernName("xxx政府");
        bussEnvIntro.setAreaIntro("<p style=\"text-align: left;\"><span style=\"font-family: 宋体;\"> &nbsp; &nbsp;区域优势</span></p>");
        bussEnvIntro.setResIntro("<p style=\"text-align: left;\"><span style=\"font-family: 宋体;\"> &nbsp; &nbsp;资源禀赋</span></p>");
        bussEnvIntro.setIndustryIntro("<p style=\"text-align: left;\"><span style=\"font-family: 宋体;\"> &nbsp; &nbsp;主导产业</span></p>");
        bussEnvIntro.setCooperateIntro("<p style=\"text-align: left;\"><span style=\"font-family: 宋体;\"> &nbsp; &nbsp;招商合作需求</span></p>");
        bussEnvIntro.setNote("");
        bussEnvIntro.setState(0L);
        bussEnvIntro.setPolicyIntro("<p style=\"text-align: left;\"><span style=\"font-family: 宋体;\"> &nbsp; &nbsp;产业政策</span></p>");
        bussEnvIntro.setCover("http://oss.xunxinsoft.com/img/yi-space-deafult.png");
        bussEnvIntro.setAddress("详细地址");
        bussEnvIntro.setPerson("张三");
        bussEnvIntro.setPhone("13100131000");
        bussEnvIntro.setRecom(2L);
        bussEnvIntro.setParkLongitude("116.443142");
        bussEnvIntro.setParkLatitude("39.921489");
        bussEnvIntro.setTop(0L);
        bussEnvIntro.setApprovalNote("");
        bussEnvIntro.setRecheckNote("");
        list.add(bussEnvIntro);
        ExcelUtil<BussEnvIntro> util = new ExcelUtil<>(BussEnvIntro.class);
        util.exportExcel(response, list, "区域营商环境管理数据");
    }


    /**
     * 数据导入
     * @param file 文件
     * @param updateSupport 更新支持
     * @return
     * @throws Exception
     */
    @Log(title = "区域营商环境介绍", businessType = BusinessType.IMPORT)
    @RequiresPermissions("business:businessIntro:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BussEnvIntro> util = new ExcelUtil<>(BussEnvIntro.class);
        List<BussEnvIntro> bussEnvIntroList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = bussEnvIntroService.importUser(bussEnvIntroList, updateSupport, operName);
        return success(message);
    }

    /**
     * 获取区域营商环境介绍详细信息
     */
    @RequiresPermissions(value = {"business:businessIntro:query","business:busfrontIntro:query"},logical = Logical.OR)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bussEnvIntroService.selectBussEnvIntroById(id));
    }

    /**
     * 新增区域营商环境介绍
     */
    @RequiresPermissions(value = {"business:businessIntro:add","business:busfrontIntro:add"},logical = Logical.OR)
    @Log(title = "区域营商环境介绍", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BussEnvIntro bussEnvIntro)
    {
        return toAjax(bussEnvIntroService.insertBussEnvIntro(bussEnvIntro));
    }

    /**
     * 修改区域营商环境介绍
     */
    @RequiresPermissions(value = {"business:businessIntro:edit","business:busfrontIntro:edit"},logical = Logical.OR)
    @Log(title = "区域营商环境介绍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BussEnvIntro bussEnvIntro)
    {
        return toAjax(bussEnvIntroService.updateBussEnvIntro(bussEnvIntro));
    }

    /**
     * 删除区域营商环境介绍
     */
    @RequiresPermissions(value = {"business:businessIntro:remove","business:busfrontIntro:remove"},logical = Logical.OR)
    @Log(title = "区域营商环境介绍", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bussEnvIntroService.deleteBussEnvIntroByIds(ids));
    }

    @Log(title = "区域营商环境介绍", businessType = BusinessType.OTHER)
    @GetMapping("/statistics")
    public AjaxResult statistics(){
        return AjaxResult.success(bussEnvIntroService.statistics());
    }

    private final static String ONLINE_INFO = "onlineInfo:";
    /**
     * 区域营商环境：business
     * 产业物理空间：industry
     * 科技产业服务港：service
     * 三大频道 线上咨询点击量添加
     */
    @GetMapping("/addClickRate")
    public AjaxResult addClickRate(@RequestParam("clickType") String clickType, @RequestParam("id") String id) {
        Integer cacheObject = redisService.getCacheMapValue(ONLINE_INFO + clickType,id);
        if (cacheObject==null){
            cacheObject = 0;
        }
        redisService.setCacheMapValue(ONLINE_INFO + clickType, id, cacheObject + 1);
        return AjaxResult.success();
    }

    /**
     * 区域营商环境：business
     * 产业物理空间：industry
     * 科技产业服务港：service
     * 三大频道 线上咨询点击量查询
     */
    @GetMapping("/getClickRate")
    public AjaxResult getClickRate(){
        Map<String,Integer> business = redisService.getCacheMap(ONLINE_INFO+"business");
        Map<String,Integer> industry = redisService.getCacheMap(ONLINE_INFO+"industry");
        Map<String,Integer> service = redisService.getCacheMap(ONLINE_INFO+"service");
        long b = 0L;
        long i = 0L;
        long s = 0L;
        for (Map.Entry<String, Integer> entry : business.entrySet()) {
            b = b + entry.getValue();
        }
        for (Map.Entry<String, Integer> entry : industry.entrySet()) {
            i = i + entry.getValue();
        }
        for (Map.Entry<String, Integer> entry : service.entrySet()) {
            s = s + entry.getValue();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("business",b);
        map.put("industry",i);
        map.put("service",s);
        return AjaxResult.success(map);
    }
}
